package com.zoro.barn.business.service.system.business.teamrole.controller;

import com.zoro.barn.business.service.system.business.team.entity.BarnTeam;
import com.zoro.barn.business.service.system.business.teamrole.service.BarnTeamRoleService;
import com.zoro.barn.business.service.system.business.team.exceptions.TeamNotExistException;
import com.zoro.barn.business.service.system.business.team.service.BarnTeamService;
import com.zoro.barn.business.service.system.business.teamrole.controller.dto.TeamAddRolesDto;
import com.zoro.barn.business.service.system.business.teamrole.entity.BarnTeamRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("team-role")
public class BarnTeamRoleController {


    private final BarnTeamService barnTeamService;
    private final BarnTeamRoleService barnTeamRoleService;

    public BarnTeamRoleController(BarnTeamService barnTeamService, BarnTeamRoleService barnTeamRoleService) {
        this.barnTeamService = barnTeamService;
        this.barnTeamRoleService = barnTeamRoleService;
    }

    /**
     * 向 team 添加角色
     *
     * @param dto {@link TeamAddRolesDto}
     */
    @RequestMapping("/addRoleIds")
    public void addRoleIds(@RequestBody TeamAddRolesDto dto) {

        BarnTeam data = this.barnTeamService.getById(dto.getPid());
        if (data == null) {
            throw new TeamNotExistException(dto.getPid());
        }
        this.barnTeamRoleService.addRole(dto.getPid(), dto.getRoleIds());

    }

    /**
     * 删除 team 与角色关系
     *
     * @param dto role and user's id
     */
    @RequestMapping("delete")
    public void delete(@RequestBody BarnTeamRole dto) {

        if (StringUtils.isAnyBlank(dto.getRoleId(), dto.getTeamId())) {
            return;
        }
        this.barnTeamRoleService.delete(dto.getRoleId(), dto.getTeamId());
    }
}
